package org.jeecg.modules.wangyue.payment.alipay.controller;

import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.payment.alipay.request.AlipayTransferRequest;
import org.jeecg.modules.wangyue.payment.alipay.service.IAlipayService;
import org.jeecg.modules.wangyue.service.IIndentJsService;
import org.jeecg.modules.wangyue.service.IWithdrawalReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author dhw
 */

@Slf4j
@Api(tags = "支付宝支付")
@RestController
@RequestMapping("/wangyue/sys/payment/alipay/")
public class AliPaySysController {

    @Autowired
    IAlipayService iAlipayService;
    @Autowired
    IIndentJsService indentJsService;
    @Autowired
    IWithdrawalReviewService withdrawalReviewService;

    /**
     * 单笔转账到支付宝账户
     */
    @PostMapping("/Alipay_FundTransUniTransfer")
    @ResponseBody
    public Result<T> fundTransUniTransfer(@RequestBody AlipayTransferRequest alipayTransferRequest) {
        AlipayFundTransUniTransferResponse response = iAlipayService.fundTransUniTransfer(alipayTransferRequest);
        log.info(response.getBody());
        if (response.isSuccess()) {
            log.info(response.getSubMsg());
            //提现成功需调用金额扣减接
            Result<?> result;
            int i = 0;
            while (i < 3) {
                if (RoleConstant.yonghu.equals(alipayTransferRequest.getReqUserRoleInfoModel().getUserType())) {
                    //用户端提现金额扣减
                    result = withdrawalReviewService.userWithdrawal(alipayTransferRequest.getReqUserRoleInfoModel());
                } else {
                    // 技师提现金额扣减
                    result = withdrawalReviewService.jsWithdrawalPass(alipayTransferRequest.getReqUserRoleInfoModel().getUserId());
                }
                if (result.getCode() != 200) {
                    i++;
                } else {
                    break;
                }
            }
            return Result.OK(response.getSubMsg());
        } else {
            log.error(response.getSubMsg());
            return Result.error(response.getSubMsg());
        }
    }
}
